<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class signController extends BaseController {



	public function _initialize() {
		parent :: _initialize();
		$this->_top_menu_list = array (
			'left' => array (
				array (
					'type' => 'home',
					'href' => U('Mob/Activity/activitymap'
				)
			),


		), 'center' => array (
			'title' => '知道'
		));
		if (is_login()) {


	  $this->_top_menu_list['right'][] = array (
					'type' => 'edit',
					'href' => U('Mob/Activity/creator'
				));

		} else {
			$this->_top_menu_list['right'][] = array (
				'type' => 'edit',
				'info' => '登录后才能操作！'
			);

		$this->redirect('Member/index');


		}
		$this->UserInfo = query_user(array('nickname', 'avatar64', 'uid'), $v['uid']);
		$this->setMobTitle('知道');
		$this->assign('top_menu_list', $this->_top_menu_list);

	}

	/**
	 * 主页面显示
	 */
	public function index() {

   $uid = get_uid();
   if(D('Teacher')->is_teacher($uid))
   {
    $this->redirect('Teacher/myClasses');
   }
   $student = D('Student');
   $studentInfo = $student->queryStudent($uid);
   if($studentInfo)
   {
    switch($studentInfo['verificFlag'])
    {
     case 0:  //待审核
       $this->redirect('Student/register',array('studentId'=>$studentInfo['studentId']));
     break;

     case 1:  //审核通过
     $this->redirect('Sign/signOperate',array('studentId'=>$studentInfo['studentId']));
     break;
     case 2:  //审核未通过
     print_r('verific Failure!');
     break;
    }
   }else
   {
   //$this->redirect('Student/register',array('studentId'=>$studentInfo['studentId']));
   $this->display();
   }

	}

   public function signOperate()
   {

    if(IS_POST)
    {
    $uid  = get_uid();
    $sign = D('Sign');
    $is_sign = $sign->is_existSign($uid);
    if($is_sign)
    {

     $data['status']=0;
   	 $data['msg']='已经签过了，不能重复操作!';

    }else
    {
     $latitude = I('post.latitude','43.886841','float'); //从请求中取纬度
     $longitude = I('post.longitude','125.3245','float');  //从请求中经度
     $schooId  = I('post.schooId','10183','int');
     $student = D('Student')->queryStudent($uid);
     $slatitude = $student['latitude'];
     $slongitude = $student['longitude'];
      $openid = $student['openId'];
     $studentId =$student['studentId'];


     $distance =  $sign->GetDistance($latitude,$longitude,$slatitude,$slongitude);
     $distanceFlg = ($distance<100)?true:false;
     // $studentInfo['schooId'],$studentInfo['majorId']
     $is_signTime = $sign->is_SignTime($student['schooId'],$student['majorId']);
    if(!$distanceFlg)
    {
    $sign->creatorRecord($uid,$openid,$latitude,$longitude,$studentId,$schooId,2,$slongitude,$slatitude,$distance);

     $data['status']=0;
   	 $data['msg']='不属于签到位置!';
     // $this->ajaxReturn(array('status'=>0,'msg'=>$distance));

    }elseif(!$is_signTime)
    {
    $sign->creatorRecord($uid,$openid,$latitude,$longitude,$studentId,$schooId,3,$slongitude,$slatitude,$distance);
     $data['status']=0;
   	 $data['msg']='非正常签到时间!';

     }
     else
    {


    $res = $sign->creatorRecord($uid,$openid,$latitude,$longitude,$studentId,$schooId,1,$slongitude,$slatitude,$distance);

    if($res)
    {
     $data['status']=1;
   	 $data['msg']='签到成功!';
   	 $data['name']=$student['name'];
   	 $data['studentId']=$studentId;
   	 $data['signTime']=date('Y-m-d H:i:s', $res['signTime']);

    }else
    {
     $data['status']=0;
   	 $data['msg']='操作失败!';
   	// $data['msg']=M()->getLastSql();
    }
    }
    }

   $this->ajaxReturn($data);
    }else
    {
    $studentId = I('studentId','','int');
    $uid  = get_uid();
    $student = D('Student');
    $studentInfo = $student->queryStudent($uid);
    if(empty($studentInfo))   //如果没有注册过，直接跳到注册页面
    {
     $this->redirect('Student/register',array('errmsg'=>'请绑定班组后，再签到操作！'));
    }
    if($studentInfo['longitude']==null||$studentInfo['latitude'==null])
    {
     $this->redirect('Student/updateDormLocation',array('errmsg'=>'请设置寝室位置后，再签到操作！'));
    }

    $is_Sign = D('Sign')->is_existSign($uid);
    if($is_Sign)
    {
    $is_Sign=1;
    }else
    {
    $is_Sign=0;
    }
    $is_signDate = D('Sign')->is_SignTime($studentInfo['schooId'],$studentInfo['majorId']);  //判断是否为签到时间
    $signTime = D('Sign')->get_allowSignTime($studentInfo['schooId'],$studentInfo['majorId']);
    $signTime['beginSingTime']=substr($signTime['beginSingTime'],0,2).':'.substr($signTime['beginSingTime'],2,2);
    $signTime['endSingTime']=substr($signTime['endSingTime'],0,2).':'.substr($signTime['endSingTime'],2,2);
    $is_signDate = ($is_signDate)?1:0;  //判断签到状态
    $this->assign('signTime',$signTime);
    $this->assign('is_signDate',$is_signDate);
    $this->assign('is_sign',$is_Sign);
    $this->assign('student',$studentInfo);
    $this->display();
    }
   }


   public function historySign()
   {
    $uid=get_uid();
   	$sign = D('Sign');
   	$student = D('Student')->queryStudent($uid);
   	$leave = D('Leave');
    $leaveRecord = $leave->personLeaveRecordByDate($uid,$student['schooId']);
   	if($student['is_sign']==1)   //判断类型为1的话，代表需要签到
   	{
   	$signRecord= $sign->get_personalSign1($uid,30);
   	}

   $this->assign('signRecord',$signRecord);
   $this->assign('leaveRecord',$leaveRecord);
   $this->assign('default',date('Y-m-d'));
   $this->display();
   }
  public function signTimeSetting()
  {
  	if(IS_POST)
  	{
  	$schooId = I('post.schooId',null,'int');
  	$orgaId  = I('post.orgaId',null,'int');
  	$signStartTime= I('post.signStartTime',null,'string');
  	$signEndTime= I('post.signEndTime',null,'string');
  	$isWeekend= I('post.isWeekend','false','string');
  	$ext= I('post.exit',null,'string');
    $teacherUid = get_uid();
    $is_auth = D('Orga')->is_AuthVerify($teacherUid,$orgaId,2,$schooId);
    if($is_auth)
    {
    $data['signStartTime']=$signStartTime;
    $data['signEndTime']=$signEndTime;
    $data['isWeekend']=$isWeekend;
    $data['exit']=$ext;

    $res = D('SignParam')->updateParam($schooId,$orgaId,$data);
    if($res)
    {
     $data = array('status'=>1,'msg'=>'操作成功!');
    }else
    {
   $data = array('status'=>0,'msg'=>'操作失败!');
    }

    }else
    {
    $data = array('status'=>0,'msg'=>'您没有操作权限!');
    }
    $this->ajaxReturn($data);
  	}else
  	{
  	$uid = get_uid();
  	$orga = D('Orga')->get_TeacherOrga($uid);///
  	$signParam = D('SignParam')->get_param($orga['schooId'],$orga['orgaId']);
  	$this->assign('orga',$orga);
  	$this->assign('signParam',$signParam);
  	$this->display();
  	}

  }

  public function signStatistics()
  {
  $schooId = I('get.schooId',10183,'int');
 $SignSituation = D('Sign')->get_SignSituation($schooId);

  $this->assign('SignSituation',$SignSituation);
  $this->display();



  }









}



